package com.cuitrip.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.SupportMapFragment;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.cuitrip.business.location.map.GaoDeMapActivity;
import com.cuitrip.service.R;

/**
 * Created by MiJack on 2015/11/4.
 */
public class GaoDeMapController implements MapController {
    SupportMapFragment fragment;
    private AMap map;

    @Override
    public void init(final FragmentActivity activity, final String meetingPlace, final String latitude, final String longtitude) {
        if (TextUtils.isEmpty(meetingPlace)||TextUtils.isEmpty(latitude)||TextUtils.isEmpty(longtitude)){
            return;
        }
        fragment=new SupportMapFragment();
        activity.getSupportFragmentManager().beginTransaction().replace(R.id.mapViewContainer, fragment).commit();
        map = fragment.getMap();
        if (map!=null){
            UiSettings settings = map.getUiSettings();
            settings.setAllGesturesEnabled(false);
            settings.setZoomControlsEnabled(false);
        }
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateMap(activity,meetingPlace, latitude, longtitude);
            }
        });
    }

    private void updateMap(FragmentActivity activity, String meetingPlace,String latitude,String longtitude) {
        double lat=Double.valueOf(latitude);
        double lng=Double.valueOf(longtitude);
        if (TextUtils.isEmpty(meetingPlace)||TextUtils.isEmpty(latitude)||TextUtils.isEmpty(longtitude)){
            return;
        }
        if (fragment==null) {
            fragment = new SupportMapFragment();
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.mapViewContainer, fragment).commitAllowingStateLoss();
            activity.getSupportFragmentManager().executePendingTransactions();
            map = fragment.getMap();
        }
        if (map==null){
            map=fragment.getMap();
        }
        if (map==null){
            return;
        }
        UiSettings settings = map.getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setZoomControlsEnabled(false);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 18));
        map.clear();
        map.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                .position(new LatLng(lat, lng
                )).snippet(meetingPlace));
    }

    @Override
    public void onPlaceSelect(FragmentActivity activity, String meetingPlace,String latitude,String longtitude) {
        updateMap(activity,meetingPlace,latitude,longtitude);
    }

    @Override
    public void showPlace(Context context, double lat, double lng, String meetingPlace) {
        GaoDeMapActivity.startShow(context, lat, lng, meetingPlace);
    }

    @Override
    public void searchMap(Activity activity, String lat, String lng, String meetingPlace) {
        Intent intent=new Intent(activity,GaoDeMapActivity.class);
        if (!TextUtils.isEmpty(lat)&&!TextUtils.isEmpty(lng)){
            intent.putExtra(VALUE_NAME, meetingPlace);
            try {
                intent.putExtra(VALUE_LAT, Double.valueOf(lat));
                intent.putExtra(VALUE_LNG, Double.valueOf(lng));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        intent.putExtra(VALUE_TYPE,TYPE_SERACH);
        GaoDeMapActivity.startSearch(activity,intent);
    }
}
